package no.nordicsemi.android.nrfmesh.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import no.nordicsemi.android.nrfmesh.node.WaveformSegmentFragment;

import java.util.ArrayList;
import java.util.List;

public class SegmentPagerAdapter extends FragmentStateAdapter {

    private final List<Integer> segmentIndices = new ArrayList<>();

    public SegmentPagerAdapter(@NonNull FragmentActivity fa) {
        super(fa);
    }

    public void addSegment() {
        segmentIndices.add(segmentIndices.size() + 1);
        notifyItemInserted(segmentIndices.size() - 1);
    }

    public void removeSegment(int position) {
        if (position >= 0 && position < segmentIndices.size()) {
            segmentIndices.remove(position);
            notifyItemRemoved(position);
        }
    }

    public int getSegmentCount() {
        return segmentIndices.size();
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return WaveformSegmentFragment.newInstance(segmentIndices.get(position));
    }

    @Override
    public int getItemCount() {
        return segmentIndices.size();
    }
}

